package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.OrderInfoVO;
import com.ruoyi.system.domain.vo.OrderShiftCostRequest;
import com.ruoyi.system.mapper.OrderInfoMapper;
import com.ruoyi.system.mapper.StoreInventoryMapper;
import com.ruoyi.system.service.AddressService;
import com.ruoyi.system.service.IOrderInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private AddressService addressService;
    @Resource
    private StoreInventoryMapper storeInventoryMapper;
    @Override
    public OrderInfo selectOrderById(int orderId) {
        return orderInfoMapper.selectOrderById(orderId);
    }

    @Override
    public List<OrderInfo> selectOrderList(OrderInfo orderInfo) {
        return orderInfoMapper.selectOrderList(orderInfo);
    }

    @Override
    public int insertOrder(OrderInfoVO orderInfo) {
        if( Objects.isNull(orderInfo.getStrategy())) {
            throw new ServiceException("请选择配送策略");
        }
        //获取收货地址信息
        GeoCodingResult.Location billLocation = addressService.getPosition(orderInfo.getBillAddr(), orderInfo.getBillCity());
        if(Objects.isNull(billLocation)) {
            throw new ServiceException("收货地址信息有误");
        }
        Pair<Double, Double> billLocationPair = Pair.of(billLocation.getLat(), billLocation.getLng());
        List<Store> stores = storeInventoryMapper.selectByItemId(orderInfo.getItemId());
        if(CollectionUtils.isEmpty(stores)) {
            throw new ServiceException("无货");
        }
        Store closetStore = null;
        if(1 == orderInfo.getStrategy()) {
            //距离优先
            closetStore = stores.get(0);
        } else if(2 == orderInfo.getStrategy()) {
            //评分最高
            closetStore = stores.get(0);
        } else if (3 == orderInfo.getStrategy()) {
            closetStore = stores.get(1);
        } else {
            closetStore = stores.get(2);
        }
        //扣减库存
        StoreInventory storeInventory = storeInventoryMapper.selectInventoryByStoreAndItem(closetStore.getId(), orderInfo.getItemId());
        if(storeInventory.getNum() >= orderInfo.getQuantity()) {
            storeInventoryMapper.updateInventory(new StoreInventory(closetStore.getId(), orderInfo.getItemId(), storeInventory.getNum()-orderInfo.getQuantity()));
        } else {
            throw new ServiceException("库存不足");
        }
        String userName = SecurityUtils.getUsername();
        orderInfo.setOrderId(IdUtils.simpleUUID());
        orderInfo.setCreateBy(userName);
        orderInfo.setCreateTime(new Date());
        orderInfo.setCreateDate(new Date());
        orderInfo.setUpdateTime(new Date());
        orderInfo.setUpdateBy(userName);
        OrderInfo orderInfo1 = new OrderInfo();
        BeanUtils.copyProperties(orderInfo, orderInfo1);
        orderInfoMapper.insertOrder(orderInfo1);
        //创建派送单
        Task task = new Task();
        task.setTaskId(IdUtils.simpleUUID());
        task.setOrderId(orderInfo1.getOrderId());
        task.setStoreId(closetStore.getId());
        task.setStatus("0");
        task.setShipName(orderInfo.getBillName());
//        task.setShipTel(orderInfo.getBillTel());
        task.setShipPro(orderInfo.getBillPro());
        task.setShipCity(orderInfo.getBillCity());
        task.setShipDis(orderInfo.getBillDis());
        task.setShipAddr(orderInfo.getBillAddr());
        task.setBillName(orderInfo.getBillName());
        task.setBillPro(orderInfo.getBillPro());
        task.setBillCity(orderInfo.getBillCity());
        task.setBillDis(orderInfo.getBillDis());
        task.setBillAddr(orderInfo.getBillAddr());
        task.setTotalPrice(orderInfo.getTotalPrice());
        task.setNote(orderInfo.getNote());
        return 1;
    }

    private Store getClosetStore(Pair<Double, Double> startPoint, List<Store> stores) {
        double closetDis = 0;
        Store closetStore = null;
        for(Store store : stores) {
            if(store.getLatitude() == null || store.getLongitude() == null) {
                continue;
            }
            Pair<Double, Double> endPoint = Pair.of(store.getLatitude(), store.getLongitude());
            double dis = addressService.distance(startPoint, endPoint);
            if(closetDis == 0 || dis < closetDis) {
                closetDis = dis;
                closetStore = store;
            }
        }
        return closetStore;
    }

    @Override
    public int updateOrder(OrderInfo orderInfo) {
        orderInfo.setUpdateBy(SecurityUtils.getUsername());
        orderInfo.setUpdateTime(new Date());
        return orderInfoMapper.updateOrder(orderInfo);
    }

    @Override
    public Object shiftCostCal(OrderShiftCostRequest request) {
        return null;


    }
} 